﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.Security;
using System.Security.Cryptography;

namespace Aandelendelen.Security
{
	public class Hash
	{
		public static string GetMD5Hash(string input)
		{
			MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();

			byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

			StringBuilder sBuilder = new StringBuilder();

			for (int i = 0; i < data.Length; i++)
			{
				sBuilder.Append(data[i].ToString("x2"));
			}

			return sBuilder.ToString();
		}

		public static string GetRandomPassword()
		{
			int length = 6 + (new Random().Next(3));

			byte[] random = new byte[length];

			StringBuilder newPassword = new StringBuilder();

			RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();

			crypto.GetBytes(random);

			for (int i = 0; i < length; i++)
			{
				int rand = Convert.ToInt32(random[i]) % 26;

				newPassword.Append(Convert.ToChar('a' + rand));
			}

			return newPassword.ToString();
		}

	}
}